package cn.ren.service;

import cn.ren.constant.MessageConstant;
import cn.ren.dao.MemberDao;
import cn.ren.dao.OrderDao;
import cn.ren.dao.OrderSettingDao;
import cn.ren.entity.OrderSettingList;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.Member;
import cn.ren.pojo.Order;
import cn.ren.pojo.OrderSetting;
import cn.ren.util.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-02-25 23:22
 **/
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public void add(List<OrderSetting> list) {
        //遍历对象集合
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //判断该预约的日期是否已经存在预约了
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){
                    //数据库里有这一天的预约，调用Dao去更新数据。
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //数据库里没有这一天的预约，调用Dao去保存
                    orderSettingDao.add(orderSetting);
                }

            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String start = date + "-1";
        String end = date + "-31";
        //根据该时间段去查询数据库
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(start,end);
        System.out.println(list);
        //遍历list，保存到Map中
        List<Map> res = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            res.add(map);
        }
        return res;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断这一天是否已经预定了
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Page<OrderSettingList> list = getPage(currentPage,pageSize,queryString);
        if (list.size() < 1){
            //往前翻一页
            currentPage = currentPage -1;
            if (currentPage >= 0){
                list = getPage(currentPage,pageSize,queryString);
            }
        }
        return new PageResult(list.getTotal(),list.getResult());
    }
    public Page<OrderSettingList> getPage(Integer currentPage,Integer pageSize,String queryString){
        PageHelper.startPage(currentPage,pageSize);
        Page<OrderSettingList> list = orderSettingDao.findListByCondition(queryString);
        return list;
    }

    @Override
    public void del(int id) {
        orderSettingDao.del(id);
    }

    @Override
    public Result addNewOrderByBackEnd(Member member, Integer[] setmealIds) throws Exception {
        //获取预约时间
        Date orderDate = member.getRegTime();
        //查询改数据库，这一天是否可以预约
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderDate);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断这一天预约人数是否满了
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断是否是会员
        Member memberByTel = memberDao.findMemberByTel(member.getPhoneNumber());
        if (memberByTel != null) {
            //获取他这一天的预约情况，如果有重复的套餐，则跳过，并返回提醒消息
            String mes = "";
            for (int i = 0; i < setmealIds.length; i++) {
                Order order = new Order(
                        memberByTel.getId(),
                        orderDate,
                        null,
                        null,
                        setmealIds[i]);
                List<Order> list = orderDao.findOrderByCondition(order);
                if (list != null && list.size() > 0){
                    //重复预约了,获取套餐名
                    mes = mes + list.get(0).getSetmeal().getName()+",";
                }else {
                    //没有重复，新增套餐
                    order.setOrderType(Order.ORDERTYPE_TELEPHONE);
                    order.setOrderStatus(Order.ORDERSTATUS_NO);
                    orderDao.add(order);
                }
            }
           return new Result(true,mes+"这些套餐已经预约过了，其他的已预约成功");
        }
        if (memberByTel == null){
            //添加到会员
            member.setRegTime(new Date());
            memberDao.add(member);
            //将预约保存到数据库
            for (int i = 0; i < setmealIds.length; i++) {
                Order order = new Order(member.getId(),
                        orderDate, Order.ORDERTYPE_TELEPHONE,
                        Order.ORDERSTATUS_NO, setmealIds[i]);
                orderDao.add(order);
            }
        }
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }
}
